//
//  ViewController.m
//  09--HomeWork
//
//  Created by 冀永金 on 16/7/26.
//  Copyright © 2016年 冀永金. All rights reserved.
//

#import "ViewController.h"
#import "VCOfProvice.h"
@interface ViewController () <UITableViewDelegate,UITableViewDataSource>
@property (nonatomic, strong) NSMutableArray *proArr;
@property (nonatomic, strong) UITableView *tableView;
@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.navigationItem.title = @"省份";
    NSString *datastring = [NSString stringWithContentsOfFile:@"/Users/jiyongjin/Desktop/area.txt" encoding:NSUTF8StringEncoding error:nil];
    NSArray *arrOfPlace = [datastring componentsSeparatedByString:@"\n"];
   
    //创建一个省数组
   self.proArr = [NSMutableArray array];
    for (NSString *name in arrOfPlace) {
        if (![name hasPrefix:@" "]) {
            //建立省字典
            NSMutableDictionary *dicOfPro = [NSMutableDictionary dictionary];
            [dicOfPro setObject:name forKey:@"proName"];
            //建立市数组
            NSMutableArray *cityArr = [NSMutableArray array];
            [dicOfPro setObject:cityArr forKey:@"cityArr"];
            [_proArr addObject:dicOfPro];
            
        }
        else if ([name hasPrefix:@"  "] && ![name hasPrefix:@"    "]) {
            //建立市字典
            NSMutableDictionary *dicOfCity = [NSMutableDictionary dictionary];
            [dicOfCity setObject:name forKey:@"cityName"];
            //建立区数组
            NSMutableArray *areaArr = [NSMutableArray array];
            [dicOfCity setObject:areaArr forKey:@"areaArr"];
            NSMutableDictionary *cityDic = [_proArr lastObject];
            NSMutableArray *marrCity = cityDic[@"cityArr"];
            [marrCity addObject:dicOfCity];
                    }else {
            NSMutableDictionary *proDic = [_proArr lastObject];
            NSMutableArray *cityArr = proDic[@"cityArr"];
            NSMutableDictionary *cityDic = [cityArr lastObject];
            NSMutableArray *areaArr = cityDic[@"areaArr"];
            [areaArr addObject:name];
        }
    }
       [self createTableView];
   
    // Do any additional setup after loading the view, typically from a nib.
}
- (void)createTableView {
    self.tableView = [[UITableView alloc]initWithFrame:self.view.bounds style:UITableViewStylePlain];
    [self.view addSubview:self.tableView];
    _tableView.delegate =self;
    _tableView.dataSource= self;
    //[_tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:@"pool"];
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return _proArr.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"pool"];
    if (cell == nil) {
        cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:@"pool"];
     }
    NSString *str1 =  [_proArr[indexPath.row] objectForKey:@"proName"];
    NSArray *arr = [str1 componentsSeparatedByString:@" "];
    cell.textLabel.text = [arr firstObject];
    cell.detailTextLabel.text = [arr lastObject];
     cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
    
       return cell;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    VCOfProvice *province = [[VCOfProvice alloc]init];
    [self.navigationController pushViewController:province animated:YES];
    NSArray *arrOfPrinvice = [_proArr[indexPath.row] objectForKey:@"cityArr"];
   // NSLog(@"%@",[[arrOfPrinvice lastObject] objectForKey:@"cityName"]);
    province.arrOfBig = arrOfPrinvice;
    NSString *str1 =  [_proArr[indexPath.row] objectForKey:@"proName"];
    NSArray *arr = [str1 componentsSeparatedByString:@" "];
    province.name = [arr firstObject];
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end
